package com.fkcai.hansoul.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fkcai.hansoul.model.entity.UserFollow;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 操作用户关注表
 */
@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollow> {
	
	/**
	 * 根据用户 id 获取用户关注人数
	 *
	 * @param userId 用户 id
	 * @return 用户关注人数
	 */
	@Select("SELECT COUNT(*) FROM user_follow WHERE follower_user_id = #{userId}")
	Integer countFollowingByUserId (@Param("userId") Integer userId);
	
	
	/**
	 * 根据用户 id 获取用户粉丝数
	 *
	 * @param userId 用户 id
	 * @return 用户粉丝数
	 */
	@Select("SELECT COUNT(*) FROM user_follow WHERE followed_user_id = #{userId}")
	Integer countFansByUserId (@Param("userId") Integer userId);
	
	
	/**
	 * 根据用户 id 获取用户互关人数
	 *
	 * @param userId 用户 id
	 * @return 互关人数
	 */
	@Select("SELECT COUNT(*) FROM user_follow uf1 " +
			"INNER JOIN user_follow uf2 " +
			"ON uf1.follower_user_id = uf2.followed_user_id " +
			"AND uf1.followed_user_id = uf2.follower_user_id " +
			"WHERE uf1.followed_user_id = #{userId}")
	Integer countMutualFollowingByUserId (@Param("userId") Integer userId);
	
	
	/**
	 * 根据用户 id 获取用户关注列表
	 *
	 * @param userid 用户 id
	 * @return 关注列表
	 */
	@Select("SELECT followed_user_id FROM user_follow WHERE follower_user_id = #{userId}")
	List<Integer> followingUsersList (@Param("userId") Integer userid);
	
	
	/**
	 * 根据用户 id 获取用户粉丝列表
	 *
	 * @param userId 用户 id
	 * @return 粉丝列表
	 */
	@Select("SELECT follower_user_id FROM user_follow WHERE followed_user_id = #{userId}")
	List<Integer> fansUsersList (@Param("userId") Integer userId);
	
	
	/**
	 * 根据用户 id 获取用户互关列表
	 *
	 * @param userId 用户 id
	 * @return 互关列表
	 */
	@Select("SELECT uf1.followed_user_id FROM user_follow uf1 " +
			"INNER JOIN user_follow uf2 " +
			"ON uf1.follower_user_id = uf2.followed_user_id " +
			"AND uf1.followed_user_id = uf2.follower_user_id " +
			"WHERE uf1.follower_user_id = #{userId}")
	List<Integer> mutualFollowingUsersList (@Param("userId") Integer userId);
}
